//
//  MPBankSearchController.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/22.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "MPBankSearchController.h"
#import "MPBankModel.h"
#import "MPBankCell.h"

@interface MPBankSearchController ()<UITextFieldDelegate>
{}
@property (nonatomic, copy) NSString *keyword;
@property (nonatomic, strong) NSArray<MPBankModel *> *dataList;
@property (nonatomic, copy) void(^completion)(MPBankModel *bank);

@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) UIImageView *searchBgImageView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UITextField *searchTextField;
@end

@implementation MPBankSearchController
+ (instancetype)vcWithCompletion:(void (^)(MPBankModel *))completion {
    MPBankSearchController *vc = [[self alloc] init];
    vc.completion = completion;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavTitle:@"选择银行" backItem:YES];
    [self setupRefresh:self.tableView option:(ATHeaderRefresh | ATHeaderAutoRefresh)];
    [self setupEmpty:self.tableView];
    [self setupViews];
}

- (void)goBack {
    [super goBack];
    self.completion = nil;
}

- (void)refreshData:(NSInteger)page {
    [MPHttpTool POST:@"business/wallet/queryBank"
              params:@{@"keyword" : SafeDicObj(self.keyword)}
             success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                 self.dataList = [MPBankModel modelArrayWithJson:result.data];
                 [self endRefresh:NO];
                 [self.tableView reloadData];
                 [self.view endEditing:YES];
             } failure:ATDefaultFailure];
}

#pragma mark - actions
- (void)clickSearch {
    self.keyword = self.searchTextField.text;
    [self headerRefreshing];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPBankCell *cell = [MPBankCell cellForTableView:tableView indexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MPBankModel *bank = self.dataList[indexPath.row];
    !self.completion ?: self.completion(bank);
    self.completion = nil;
    [self goBack];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MPBankCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60.f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    if (!headerView) {
        headerView = self.searchView;
    }
    return headerView;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self clickSearch];
    return YES;
}

#pragma mark - privite
- (void)setupViews {
    [self.searchView addSubview:self.searchBgImageView];
    [self.searchView addSubview:self.searchButton];
    [self.searchView addSubview:self.searchTextField];
    
    [self.searchBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        make.bottom.right.offset(-10);
    }];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@14);
        make.left.equalTo(self.searchBgImageView).offset(15);
        make.centerY.offset(0);
    }];
    [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchButton.mas_right).offset(5);
        make.right.equalTo(self.searchBgImageView).offset(15);
        make.centerY.equalTo(self.searchButton);
    }];
}

#pragma mark - getters

- (UIView *)searchView {
    if (!_searchView) {
        _searchView = [[UIView alloc] init];
        _searchView.backgroundColor = [UIColor colorWithRGB:0xF6F6F6];
    }
    return _searchView;
}
- (UIImageView *)searchBgImageView {
    if (!_searchBgImageView) {
        _searchBgImageView = [[UIImageView alloc] init];
        _searchBgImageView.image = [UIImage imageNamed:@""];
        _searchBgImageView.backgroundColor = [UIColor whiteColor];
    }
    return _searchBgImageView;
}
- (UIButton *)searchButton {
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchButton setImage:[UIImage imageNamed:@"icon_sousuo"] forState:UIControlStateNormal];
        [_searchButton addTarget:self action:@selector(clickSearch) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}
- (UITextField *)searchTextField {
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] init];
        _searchTextField.placeholder = @"请输入银行名称";
        _searchTextField.font = [UIFont systemFontOfSize:15.f];
        _searchTextField.textColor = [UIColor colorWithRGB:0x999999];
        _searchTextField.returnKeyType = UIReturnKeySearch;
        _searchTextField.delegate = self;
    }
    return _searchTextField;
}

@end
